package foxmob.mlts.viewcontroller.student;

import foxmob.mlts.view.student.MultipleChoicePanel;
import foxmob.mlts.view.student.MultipleChoicePanel;
import foxmob.mlts.component.FileManager;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.model.MultipleChoiceQuestion;
import foxmob.mlts.model.Question;
import foxmob.mlts.model.Section;
import foxmob.mlts.model.Subsection;
import foxmob.mlts.model.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import foxmob.mlts.view.setter.FillInTheBlankSetterPanel;
import foxmob.mlts.view.student.FillInTheBlankPanel;
import foxmob.mlts.view.student.MultipleFillInBlanksPanel;
import foxmob.mlts.view.student.MultipleFillInJPanel;
import foxmob.mlts.view.student.SubSectionPanel;
import java.awt.Dimension;
import javax.swing.BoxLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author zpf16z
 */
public class SectionScrollPanel extends JScrollPane {
    
    public ArrayList<MultipleChoicePanel> questionsPanel;


    public SectionScrollPanel() {
        //this.setLayout(new BorderLayout());
        //loadQuestions();
    }

    SectionScrollPanel(Section s) {
        loadQuestions(s);
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     *
     * @author zpf16z
     */
    public JScrollPane loadQuestions() {

        JPanel sectionPanel = new JPanel();
        
        sectionPanel.setLayout(new GridLayout(30, 1));
        int counterQuestion = 0;
        
        int subsectionLength = QuestionPaperForm.testRecord.getSections().get(0).getSubsectionLength();
        
        Section section = QuestionPaperForm.testRecord.getSections().get(0);
        
        int count = 0;
        for (int i = 0; i < subsectionLength; i++) {
            Subsection subsection = section.getSubsections().get(i);
            //p1.add(l[i]);
            int subsectionQuestionsLength = subsection.getQuestionsLength();
            for (int j = 0; j < subsectionQuestionsLength; j++) {
                MultipleChoiceQuestion q = (MultipleChoiceQuestion) subsection.getQuestions().get(j);
                
                //questionPanel[counterQuestion].loadQuestionContent(q);
                count ++;
                MultipleChoicePanel qq = new MultipleChoicePanel();
                qq.loadQuestionContent(q, count);
                //this.questionsPanel.add(qq);
       
                sectionPanel.add(qq);
                ++counterQuestion;
            }

        }
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jsp; 
        jsp = new JScrollPane(sectionPanel, v, h);
        return jsp;
    }
        
        /**
     *
     * @param section
     */
        
    public JScrollPane loadQuestions(Section testSection) {

        JPanel sectionPanel = new JPanel();
        
        int questionNumbers = 0;
        for (Subsection ss : testSection.getSubsections()){
            questionNumbers += ss.getQuestions().size();
        }
        QuestionPaperForm.questionNumber = questionNumbers;
        int subsectionLength = testSection.getSubsectionLength();
        
        sectionPanel.setLayout(new BoxLayout(sectionPanel,BoxLayout.PAGE_AXIS));
//        sectionPanel.setLayout(new GridLayout(questionNumbers + subsectionLength, 1));
        //sectionPanel.setLayout(new GridLayout(10, 1));
        int count = 0;
        for (int i = 0; i < subsectionLength; i++) {
            Subsection subsection = testSection.getSubsections().get(i);
            //p1.add(l[i]);
            //ddSubsection(subsection);
            SubSectionPanel ss = new SubSectionPanel();
            ss.load(subsection.getTitle(), subsection.getDescription());
            sectionPanel.add(ss);
            
            int subsectionQuestionsLength = subsection.getQuestionsLength();
            
            
            for (int j = 0; j < subsectionQuestionsLength; j++) {
                if (subsection.getQuestions().get(j) instanceof MultipleChoiceQuestion) {
                    
                    MultipleChoiceQuestion q = (MultipleChoiceQuestion) subsection.getQuestions().get(j);

                    //questionPanel[counterQuestion].loadQuestionContent(q);
                    MultipleChoicePanel qq = new MultipleChoicePanel();
                    //qq.setMCQ(q);
                    count++;
                    qq.loadQuestionContent(q, count);
                    //this.questionsPanel.add(qq);
                    sectionPanel.add(qq);
                } else if(subsection.getQuestions().get(j) instanceof FillInTheBlanksQuestion) {
                        System.err.println("Add an new FIB!");
                        //System.err.println("fib");
                        FillInTheBlanksQuestion q = (FillInTheBlanksQuestion) subsection.getQuestions().get(j);
                        FillInTheBlankPanel qq = new FillInTheBlankPanel();
                        //qq.loadQuestion(q, count);
                        count++;
                        qq.loadQuestion(q, count);
                        sectionPanel.add(qq);
                        
                    }else if(subsection.getQuestions().get(j) instanceof MultipleFillInBlankQuestion){
                        System.err.println("Add an new multipleFIB!");
                        //System.err.println("fib");
                        MultipleFillInBlankQuestion q = (MultipleFillInBlankQuestion) subsection.getQuestions().get(j);
                        //MultipleFillInBlanksPanel qq = new MultipleFillInBlanksPanel();
                        //qq.loadQuestion(q, count);
                        count++;
                        
                        MultipleFillInJPanel qq = new MultipleFillInJPanel();
                        qq.loadQuestion(q, count);
                        sectionPanel.add(qq);
                    }
               
                System.out.println("Creat question: " + Integer.toString(count));
            }

        }
        
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jsp; 
        jsp = new JScrollPane(sectionPanel, v, h);
        jsp.getVerticalScrollBar().setUnitIncrement(80);
        return jsp;
    }
    

    /**
     * @param args
     */
    public static void main(String[] args) {
        new SectionScrollPanel();
    }
}